const {ccclass, property} = cc._decorator;

@ccclass
export default class Alert extends cc.Component{

    /**弹出框组件 */
    alert: cc.Node = null;

    /**确定按钮 */
    btnOk: cc.Node = null;

    /**取消按钮 */
    btnCancel: cc.Node = null;

    /**弹出框标题 */
    title: cc.Label = null;

    /**弹出框内容 */
    content: cc.Label = null;

    /**点击确定后执行的回调函数 */
    onok: Function = null;


    onLoad(){

        if(cc.vv == null){
            return;
        }

        this.alert = cc.find('Canvas/alert');
        this.title = cc.find('Canvas/alert/title').getComponent(cc.Label);
        this.content = cc.find('Canvas/alert/content').getComponent(cc.Label);
        this.btnOk = cc.find('Canvas/alert/btn_ok');
        this.btnCancel = cc.find('Canvas/alert/btn_cancel');

        cc.vv.utils.addClickEvent(this.btnOk, this.node, 'Alert', 'onBtnClicked');
        cc.vv.utils.addClickEvent(this.btnCancel, this.node, 'Alert', 'onBtnClicked');

        this.alert.active = false;
        cc.vv.alert = this;
    }


    /**
     * 弹出提示框
     * @param title  弹出框标题
     * @param content 弹出框内容
     * @param onok 点击确定关闭弹出框的执行的回调函数
     * @param needCancel 是否显示取消按钮
     */
    show(title: string, content: string, onok, needCancel?: boolean){
        this.alert.active = true;
        this.onok = onok;
        this.title.string = title;
        this.content.string = content;
        if(needCancel){
            this.btnCancel.active = true;
            this.btnOk.x = -150;
            this.btnCancel.x = 150;
        }
        else{
            this.btnCancel.active = false;
            this.btnOk.x = 0;
        }
    }


    /**
     * 点击确定或取消执行的逻辑
     */
    onBtnClicked(event: cc.Event.EventCustom){
        if(event.target.name == 'btn_ok'){
            if(this.onok){
                this.onok();
            }
        }
        this.alert.active = false;
        this.onok = null;
    }

    onDestory(){
        if(cc.vv){
            cc.vv.alert = null;
        }
    }
}